﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCGenerator
{
    public class IndexCSHtml
    {
        SpaceStackStringBuilder sb = new SpaceStackStringBuilder();

        public IndexCSHtml(String NamespaceName, List<GridChoice> GridChoices, List<Searchable> SearchChoices, List<Sortable> SortChoices, String PropertyClassName, String PropertyClassPrimaryKeyName, EDMXParserV1.EntitySet SelectedEntitySet)
        {
            sb.AppendLine("@model " + NamespaceName + ".Models." + PropertyClassName + "Index");

            sb.AppendLine();
            sb.AppendLine("@{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"ViewBag.Title = ""Index"";");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");

            sb.AppendLine("");
            sb.AppendLine(@"<script type=""text/javascript"" language=""javascript"">");
            if (true)
            {
                sb.AddSpace();
                AddDocumentReadyJavascriptFunction(SearchChoices);

                foreach (Searchable SearchItem in SearchChoices)
                {
                    if (SearchItem.GridChoice.IsNull())
                    {
                        if (SearchItem.Property.Type.Contains("DateTime"))
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"function On" + SearchItem.ColumnName + @"SearchOptionResultChange() {");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"if ($(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == ""Between"") {");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#div" + SearchItem.ColumnName + @"SearchBetween"")[0].style.display = '';");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine(@"}");
                                sb.AppendLine(@"else");
                                sb.AppendLine(@"{");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#div" + SearchItem.ColumnName + @"SearchBetween"")[0].style.display = 'none';");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine(@"}");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine(@"}");
                            sb.AppendLine();
                            sb.AppendLine("function On" + SearchItem.ColumnName + @"SearchChange() ");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"if ($(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == """" && $(""#" + SearchItem.ColumnName + @"Search"").val() != """" ) {");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val(""Equals"");");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine("}");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                        }

                        if (Basics.IsNumericType(SearchItem.Property.Type))
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"function On" + SearchItem.ColumnName + @"SearchOptionResultChange() {");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"if ($(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == ""Between Inclusive""  || $(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == ""Between Exclusive"" || $(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == ""Not Between Exclusive""  || $(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == ""Not Between Inclusive"" ) {");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#div" + SearchItem.ColumnName + @"SearchBetween"")[0].style.display = '';");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine(@"}");
                                sb.AppendLine(@"else {");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#div" + SearchItem.ColumnName + @"SearchBetween"")[0].style.display = 'none';");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine(@"}");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine(@"}");
                            sb.AppendLine();
                            sb.AppendLine("function On" + SearchItem.ColumnName + @"SearchChange() ");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"if ($(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val() == """" && $(""#" + SearchItem.ColumnName + @"Search"").val() != """" ) {");
                                if (true)
                                {
                                    sb.AddSpace();
                                    sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"SearchOptionResult"").val(""="");");
                                    sb.RemoveSpace();
                                }
                                sb.AppendLine("}");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                        }
                    }

                }

                if (SortChoices.Count() > 0)
                {
                    sb.AppendLine("function SortClick(SortColumn) {");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine(@"var CurrentSortColumn = $(""#SortColumn"").val();");
                        sb.AppendLine(@"var CurrentSortDirection = $(""#SortDirection"").val();");
                        sb.AppendLine();
                        sb.AppendLine("if (CurrentSortColumn == SortColumn) {");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine();
                            sb.AppendLine("// the CurrentSortColumn is the same so flip the sort direction.");
                            sb.AppendLine();
                            sb.AppendLine("if (CurrentSortDirection == 'asc') {");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"$(""#SortDirection"").val('desc');");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.AppendLine("else");
                            sb.AppendLine("{");
                            if (true)
                            {
                                sb.AddSpace();
                                sb.AppendLine(@"$(""#SortDirection"").val('asc');");
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("}");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("// its a new sort column so initialize to asc.");
                            sb.AppendLine(@"$(""#SortDirection"").val('asc');");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine();
                        sb.AppendLine("// Keep the new sort column.d");
                        sb.AppendLine(@"$(""#SortColumn"").val(SortColumn);");
                        sb.AppendLine();
                        sb.AppendLine(@"$(""form"").submit();");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                }

                sb.AppendLine();
                sb.AppendLine("function FirstClick() {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"$(""#PagingAction"").val(""First"");");
                    sb.AppendLine(@"$(""form"").submit();");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("function LastClick() {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"$(""#PagingAction"").val(""Last"");");
                    sb.AppendLine(@"$(""form"").submit();");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("function NextClick() {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"$(""#PagingAction"").val(""Next"");");
                    sb.AppendLine(@"$(""form"").submit();");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();
                sb.AppendLine("function PreviousClick() {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"$(""#PagingAction"").val(""Previous"");");
                    sb.AppendLine(@"$(""form"").submit();");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");

                sb.RemoveSpace();
            }
            sb.AppendLine("</script>");

            sb.AppendLine();
            sb.AppendLine("<h2>Index</h2>");
            sb.AppendLine();


            sb.AppendLine("@using (Html.BeginForm()) {");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("@Html.ValidationSummary(true)");
                sb.AppendLine("@Html.HiddenFor(x => x.SortColumn);");
                sb.AppendLine("@Html.HiddenFor(x => x.SortDirection);");

                sb.AppendLine("@Html.HiddenFor(x => x.CurrentPage)");
                sb.AppendLine("@Html.HiddenFor(x => x.RecordCount)");
                sb.AppendLine("@Html.HiddenFor(x => x.RecordsPerPage)");
                sb.AppendLine("@Html.HiddenFor(x => x.PagingAction )");

                if (SearchChoices.Count > 0)
                {
                    sb.AppendLine(@"<table width=""80%"">");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("<tr>");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("<td></td>");
                            sb.AppendLine("<td></td>");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("</tr>");
                        foreach (Searchable SearchItem in SearchChoices)
                        {
                            sb.AppendLine("<tr>");
                            if (true)
                            {
                                sb.AddSpace();

                                bool UsingDDL = false;
                                foreach (var GridChoice in GridChoices)
                                {
                                    if (SearchItem.ColumnName == GridChoice.ColumnName)
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.LabelFor( model => model." + SearchItem.ColumnName + "SearchOption)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine(@"@Html.DropDownList(""" + SearchItem.ColumnName + @"Search"", Model." + SearchItem.ColumnName + "SearchOption, String.Empty )");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        UsingDDL = true;

                                    }
                                }
                                if (UsingDDL == false)
                                {
                                    if (SearchItem.Property.Type.Contains("DateTime"))
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.LabelFor( model => model." + SearchItem.ColumnName + "SearchOption)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine(@"<div style=""float:left"">");
                                            if (true)
                                            {
                                                sb.AddSpace();
                                                sb.AppendLine(@"@Html.DropDownList(""" + SearchItem.ColumnName + @"SearchOptionResult"", Model." + SearchItem.ColumnName + "SearchOption)");
                                                sb.AppendLine("@Html.TextBoxFor(model => model." + SearchItem.ColumnName + @"Search, new { @class=""date"" })");
                                                sb.AppendLine("@Html.ValidationMessageFor(model => model." + SearchItem.ColumnName + "Search)");
                                                sb.RemoveSpace();
                                            }
                                            sb.AppendLine("</div>");
                                            sb.AppendLine(@"<div id=""div" + SearchItem.ColumnName + @"SearchBetween"" style=""display:none""> and ");
                                            if (true)
                                            {
                                                sb.AddSpace();
                                                sb.AppendLine("@Html.TextBoxFor(model => model." + SearchItem.ColumnName + @"SearchBetween, new { @class=""date"" })");
                                                sb.AppendLine("@Html.ValidationMessageFor(model => model." + SearchItem.ColumnName + "SearchBetween)");
                                                sb.RemoveSpace();
                                            }
                                            sb.AppendLine("</div>");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                    }
                                    else if (Basics.IsNumericType(SearchItem.Property.Type))
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.LabelFor( model => model." + SearchItem.ColumnName + "SearchOption)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine(@"<div style=""float:left"">");
                                            if (true)
                                            {
                                                sb.AddSpace();
                                                sb.AppendLine(@"@Html.DropDownList(""" + SearchItem.ColumnName + @"SearchOptionResult"", Model." + SearchItem.ColumnName + "SearchOption)");
                                                sb.AppendLine("@Html.TextBoxFor(model => model." + SearchItem.ColumnName + @"Search)");
                                                sb.AppendLine("@Html.ValidationMessageFor(model => model." + SearchItem.ColumnName + "Search)");
                                                sb.RemoveSpace();
                                            }
                                            sb.AppendLine("</div>");
                                            sb.AppendLine(@"<div id=""div" + SearchItem.ColumnName + @"SearchBetween"" style=""display:none""> and ");
                                            if (true)
                                            {
                                                sb.AddSpace();
                                                sb.AppendLine("@Html.TextBoxFor(model => model." + SearchItem.ColumnName + @"SearchBetween)");
                                                sb.AppendLine("@Html.ValidationMessageFor(model => model." + SearchItem.ColumnName + "SearchBetween)");
                                                sb.RemoveSpace();
                                            }
                                            sb.AppendLine("</div>");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                    }
                                    else if (SearchItem.Property.Type.Contains("Boolean"))
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.LabelFor( model => model." + SearchItem.ColumnName + "SearchOption)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine(@"@Html.DropDownList(""" + SearchItem.ColumnName + @"Search"", Model." + SearchItem.ColumnName + "SearchOption)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                    }
                                    else
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.LabelFor( model => model." + SearchItem.ColumnName + "Search)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine("@Html.EditorFor(model => model." + SearchItem.ColumnName + "Search)");
                                            sb.AppendLine("@Html.ValidationMessageFor(model => model." + SearchItem.ColumnName + "Search)");
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");
                                    }
                                }

                                sb.RemoveSpace();
                            }
                            sb.AppendLine("</tr>");
                        }
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("</table>");
                    sb.AppendLine("<br />");
                    sb.AppendLine(@"<input type=""submit"" value=""Search"" />");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
            }

            sb.AppendLine("<p>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"@Html.ActionLink(""Create New"", ""Create"")");
                sb.RemoveSpace();
            }
            sb.AppendLine("</p>");
            sb.AppendLine("Records Found - @Model.RecordCount<br />");
            sb.AppendLine("<table>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("<tr>");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("<th></th>");

                    foreach (EDMXParserV1.Property item in SelectedEntitySet.EntityTypeLink.Properties)
                    {
                        if (item.IsInMasterView)
                        {
                            sb.AppendLine("<th>");
                            if (true)
                            {
                                sb.AddSpace();
                                if (item.Sortable.IsNull())
                                {
                                    sb.AppendLine(item.Name);
                                }
                                else
                                {
                                    sb.AppendLine(@"@DisplayColumnHeader(""" + item.Name + @""", Model)");
                                }
                                sb.RemoveSpace();
                            }
                            sb.AppendLine("</th>");
                        }
                        else
                        {
                            // We aren't showing this field in the MasterView.
                        }
                    }

                    sb.AppendLine("</tr>");
                    sb.RemoveSpace();
                }
                sb.AppendLine();

                sb.AppendLine("@foreach (var item in Model." + PropertyClassName + "Details) {");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("<tr>");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("<td>");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(@"@Html.ActionLink(""Edit"", ""Edit"", new { id=item." + PropertyClassPrimaryKeyName + " }) |");
                            sb.AppendLine(@"@Html.ActionLink(""Details"", ""Details"", new { id=item." + PropertyClassPrimaryKeyName + " }) |");
                            sb.AppendLine(@"@Html.ActionLink(""Delete"", ""Delete"", new { id=item." + PropertyClassPrimaryKeyName + " })");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("</td>");

                        foreach (EDMXParserV1.Property item in SelectedEntitySet.EntityTypeLink.Properties)
                        {
                            if (item.IsInMasterView)
                            {
                                bool GridChoiceUsed = false;
                                foreach (GridChoice GridChoice in GridChoices)
                                {
                                    if (GridChoice.ColumnName == item.Name)
                                    {
                                        sb.AppendLine("<td>");
                                        if (true)
                                        {
                                            sb.AddSpace();
                                            sb.AppendLine(@"@item." + GridChoice.PrimaryTable + GridChoice.DisplayColumnName);
                                            sb.RemoveSpace();
                                        }
                                        sb.AppendLine("</td>");

                                        GridChoiceUsed = true;
                                        break;
                                    }
                                }

                                if (GridChoiceUsed)
                                {
                                    // already added a combo box item.
                                }
                                else
                                {
                                    if (item.IsDateTime())
                                    {
                                        AddTableDataForDate(item);
                                    }
                                    else
                                    {
                                        AddTableData(item);
                                    }
                                }
                            }
                            else
                            {
                                // Item.IsInMasterView = false
                            }
                        }
                        sb.RemoveSpace();
                    }

                    sb.AppendLine("</tr>");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine();
                sb.RemoveSpace();
            }
            sb.AppendLine("</table>");

            sb.AppendLine("@DisplayPaging(Model)");

            AddHelperFunctions(NamespaceName, PropertyClassName);
        }

        private void AddTableData(EDMXParserV1.Property item)
        {
            sb.AppendLine("<td>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"@item." + item.Name);
                sb.RemoveSpace();
            }
            sb.AppendLine("</td>");
        }

        private void AddTableDataForDate(EDMXParserV1.Property item)
        {
            sb.AppendLine("<td>");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"@String.Format(""{0:g}"", item." + item.Name + ")");
                sb.RemoveSpace();
            }
            sb.AppendLine("</td>");
        }

        private void AddHelperFunctions(String NamespaceName, String PropertyClassName)
        {
            AddHelperDisplayColumnHeader(NamespaceName, PropertyClassName);
            AddHelperShowSortArrow();
            AddHelperDisplayPaging(NamespaceName, PropertyClassName);
        }

        private void AddHelperDisplayPaging(String NamespaceName, String PropertyClassName)
        {
            sb.AppendLine();
            sb.AppendLine("@helper DisplayPaging(" + NamespaceName + ".Models." + PropertyClassName + "Index Model )");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();

                sb.AppendLine("if (@Model.TotalPages != 0)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("<div>");
                    if (true)
                    {
                        sb.AddSpace();
                        sb.AppendLine("Page @Model.CurrentPage of @Model.TotalPages - ");
                        sb.AppendLine("@if (Model.CurrentPage == 1 || Model.RecordCount == 0 )");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("<span><< First</span>");
                            sb.AppendLine("<span>< Previous</span> ");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{ ");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(@"<span style=""background-color: Transparent;text-decoration: underline;color: blue;cursor: pointer;border:0"" onclick=""FirstClick()""><< First</span>");
                            sb.AppendLine(@"<span style=""background-color: Transparent;text-decoration: underline;color: blue;cursor: pointer;border:0"" onclick=""PreviousClick()"">< Previous</span> ");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("*");
                        sb.AppendLine("@if (Model.TotalPages == Model.CurrentPage || Model.RecordCount == 0 )");
                        sb.AppendLine("{");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine("<span>Next ></span> ");
                            sb.AppendLine("<span>Last >></span>");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.AppendLine("else");
                        sb.AppendLine("{ ");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(@"<span style=""background-color: Transparent;text-decoration: underline;color: blue;cursor: pointer;border:0"" onclick=""NextClick()"">Next ></span> ");
                            sb.AppendLine(@"<span style=""background-color: Transparent;text-decoration: underline;color: blue;cursor: pointer;border:0"" onclick=""LastClick()"">Last >></span>");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine("}");
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("</div>");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddHelperShowSortArrow()
        {
            sb.AppendLine();
            sb.AppendLine("@helper ShowSortArrow(string ColumnToCheck, String CurrentSortColumn, String CurrentDirection)");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("if (ColumnToCheck == CurrentSortColumn)");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine(@"if (CurrentDirection == ""asc"")");
                    sb.AppendLine("{ ");
                    if (true)
                    {
                        sb.AddSpace();

                        // Solid Up Triangle
                        sb.AppendLine("<span>&#9650</span>");
                        
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    sb.AppendLine("else");
                    sb.AppendLine("{");
                    if (true)
                    {
                        sb.AddSpace();

                        // Solid Down Triangle
                        sb.AppendLine("<span>&#9660</span>");
                        
                        sb.RemoveSpace();
                    }
                    sb.AppendLine("}");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.AppendLine("else");
                sb.AppendLine("{");
                if (true)
                {
                    sb.AddSpace();
                    sb.AppendLine("// columns that do not have a sort");
                    sb.AppendLine("//do nothing");
                    sb.RemoveSpace();
                }
                sb.AppendLine("}");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddHelperDisplayColumnHeader(String NamespaceName, String PropertyClassName)
        {
            sb.AppendLine();
            sb.AppendLine("@helper DisplayColumnHeader( String ColumnName, " + NamespaceName + ".Models." + PropertyClassName + "Index Model )");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"<div style=""cursor:pointer"" onclick=""SortClick('@ColumnName');"">@ColumnName @ShowSortArrow(ColumnName, Model.SortColumn, Model.SortDirection)</div>    ");
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
        }

        private void AddDocumentReadyJavascriptFunction(List<Searchable> SearchChoices)
        {
            sb.AppendLine(@"$(document).ready(function () { ");
            if (true)
            {
                sb.AddSpace();
                foreach (Searchable SearchItem in SearchChoices)
                {
                    if (SearchItem.Property.Type.Contains("DateTime"))
                    {

                        sb.AppendLine(@"$('.date').datepicker({ ");
                        if (true)
                        {
                            sb.AddSpace();
                            sb.AppendLine(@"dateFormat: ""mm/dd/yy"" ");
                            sb.RemoveSpace();
                        }
                        sb.AppendLine(@"});");
                        break;
                    }
                }

                // Add change events for the Date and numeric drop down options.
                foreach (Searchable SearchItem in SearchChoices)
                {
                    if (SearchItem.GridChoice.IsNull())
                    {
                        if (SearchItem.Property.Type.Contains("DateTime"))
                        {
                            AddOnChangeDateDropdownSearchOptionJavascript(SearchItem);
                        }

                        if (Basics.IsNumericType(SearchItem.Property.Type))
                        {
                            AddOnChangeNumericDropdownSearchOptionJavascript(SearchItem);
                        }
                    }
                }

                foreach (Searchable SearchItem in SearchChoices)
                {
                    if (SearchItem.GridChoice.IsNull())
                    {
                        if (SearchItem.Property.Type.Contains("DateTime"))
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"On" + SearchItem.ColumnName + @"SearchOptionResultChange();");
                        }

                        if (Basics.IsNumericType(SearchItem.Property.Type))
                        {
                            sb.AppendLine();
                            sb.AppendLine(@"On" + SearchItem.ColumnName + @"SearchOptionResultChange();");
                        }
                    }
                }
                sb.RemoveSpace();
            }
            sb.AppendLine(@"});");
        }

        private void AddOnChangeNumericDropdownSearchOptionJavascript(Searchable SearchItem)
        {
            sb.AppendLine();
            sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"SearchOptionResult"").change(function () {");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"On" + SearchItem.ColumnName + @"SearchOptionResultChange();");
                sb.RemoveSpace();
            }
            sb.AppendLine(@"});");
            sb.AppendLine();
            sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"Search"").change(function () {");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("On" + SearchItem.ColumnName + @"SearchChange();");
                sb.RemoveSpace();
            }
            sb.AppendLine("});");
        }

        private void AddOnChangeDateDropdownSearchOptionJavascript(Searchable SearchItem)
        {
            sb.AppendLine();
            sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"SearchOptionResult"").change(function () {");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine(@"On" + SearchItem.ColumnName + @"SearchOptionResultChange();");
                sb.RemoveSpace();
            }
            sb.AppendLine(@"});");
            sb.AppendLine();
            sb.AppendLine(@"$(""#" + SearchItem.ColumnName + @"Search"").change(function () {");
            if (true)
            {
                sb.AddSpace();
                sb.AppendLine("On" + SearchItem.ColumnName + @"SearchChange();");
                sb.RemoveSpace();
            }
            sb.AppendLine("});");
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
}
